package myJs.user.workflow.featureSelection.uniAna

import myJs.api.Api
import myJs.cps._
import myJs.myPkg.jquery._
import myJs.tool.Tool
import myJs.tool.Tool.messages
import shared.pojo.Pojo.LoginUserData
import com.softwaremill.quicklens._

import scala.collection.immutable.SeqMap
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import myJs.Implicits._
import myJs.myPkg.reactHookForm._
import myJs.user.FeatureSelectionTool
import myJs.user.workflow.featureSelection.FeatureSelection
import shared.pojo.WorkflowMission._
import myJs.Implicits._

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._

/** Created by yz on 3/11/2020
  */
object Cp {

  case class Props(
      fieldArray: UseFieldArrayRT,
      fsData: FsData,
      fsDataChange: FsData => SyncIO[Unit],
      fsDataModifyF: (FsData => FsData) => SyncIO[Unit],
      showIdStr: String,
      extraData: ExtraData
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def uniAnaDataChange(curI: Int) = (uniAnaData: EachUniAnaData) => {
        SyncIO{} >>{
          val newFsData = props.fsData.modify(_.uniAna.index(curI)).setTo(uniAnaData)
          props.fsDataChange(newFsData)
        }
      }
      def uniAnaDataModifyF(curI: Int) = (uniAnaData: EachUniAnaData => EachUniAnaData) => {
        SyncIO{} >> {
          val newFsData = modifyLens[FsData](_.uniAna.index(curI)).using(uniAnaData)
          props.fsDataModifyF(newFsData)
        }
      }

    }

    props.fieldArray.fields.toList.zipWithIndex.toVdomArray { case (item, curI) =>
      val data = props.fsData.uniAna.getOrElse(curI, EachUniAnaData())
      val displayMod = if (props.showIdStr == data.nodeId) display.block else display.none
      div(
        key := s"${item.id}",
        className := "myArg",
        displayMod,
        UniAnaCp.Props(
          curI = curI,
          data = data,
          dataChange = FTool.uniAnaDataChange(curI),
          dataModifyF = FTool.uniAnaDataModifyF(curI),
          extraData = props.extraData,
          fsData = props.fsData
        ).render
      )
    }

  }

}
